package com.framework.core.easyui.domain;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * EasyUI tree模型
 * 
 * @author
 * 
 */
public class Tree implements java.io.Serializable {

	private String id;
	private String text;
	private String state = "open";// open,closed
	private boolean checked = false;
	private Object attributes;
	private List<Tree> children = new ArrayList<Tree>();
	private String iconCls;
	private String parentId;

	/**
	 * 树子节点递归遍历
	 * @param list
	 * @return
	 */
	public static List<Tree> getRoot(List<Tree> list){
		List<Tree> treeList = new ArrayList<Tree>();
		for(Tree tree : list){
			if(StringUtils.isEmpty(tree.getParentId())){
				initChild(tree,list);
				treeList.add(tree);
			}
		}
		return treeList;
	}

	/**
	 * 节点遍历
	 * @param tree
	 * @param list
	 */
	private static void initChild(Tree tree,List<Tree> list){
		for(Tree node : list){
			if(tree.getId().equals(node.getParentId())){
				initChild(node,list);
				tree.getChildren().add(node);
				tree.setState("closed");
			}
		}
	}


	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public Object getAttributes() {
		return attributes;
	}

	public void setAttributes(Object attributes) {
		this.attributes = attributes;
	}

	public List<Tree> getChildren() {
		return children;
	}

	public void setChildren(List<Tree> children) {
		this.children = children;
	}

	public String getIconCls() {
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

}
